### Replicates Figure 2(B) for Study #3 in the paper ###



#####  Analysis starts here #################
library(RItools)
library(xtable)
setwd("~/Dropbox/Data/Paper-GDN/Analysis-Paper-01/Replic") #change to local folder
savedir <- getwd()
load("S03-dataTurkey.RData")

### Examine Randomization
table(d$TO,d$Tcond)

### Main effects of otherness
lm(OUTsupport~TO,data=d) 
lm(OUTsupport~TO,data=subset(d,attention==T)) 

### Main effect of conditionality
lm(OUTsupport~Tcond,data=d) #expected positive increase 
lm(OUTsupport~Tcond,data=subset(d,attention==T)) 


### Interaction effects
summary(lm(OUTsupport~Tcond*TO,data=d))
summary(lm(OUTsupport~Tcond*TO+attention,data=d))
summary(lm(OUTsupport~Tcond*TO,data=subset(d,attention==T)))


### Function for Robust SE ##
robse <- function(x,sig=0.95){
	require(sandwich)
	require(car)
	robSE <- sqrt(diag(vcovHC(x,type="HC2")))
	terms <- length(coef(x))
	robP <- rep(NA,terms)
	for(jj in 1:terms){
			robP[jj] <- linearHypothesis(x,diag(terms)[jj,],vcov=vcovHC(x,type="HC2"))$Pr[2]}
	upr <- coef(x) + qnorm(sig) * robSE
	lwr <- coef(x) - qnorm(sig) * robSE
	out <- cbind(robse=robSE,upr=upr,lwr=lwr,robpval=robP)
	return(out)
}

### THe main regression ####
reg02 <- lm(OUTsupport~TO*Tcond,data=subset(dturk,CCTbeneficiary==F&attention==T))

print(summary(reg02))

other.order <- c("Racial","Control","Regional")
cond.order <- c("Unconditional","Conditional")

Puncond <- data.frame(predict(reg02,newdata=data.frame(
			Tcond=rep("_Uncond",3),
			TO=other.order),
			interval="confidence"))
Pcond <- data.frame(predict(reg02,newdata=data.frame(
			Tcond=rep("Cond",3),
			TO=other.order),
			interval="confidence"))

to.plot <- rbind(Conditional=Pcond$fit,
				Unconditional=Puncond$fit)
colnames(to.plot)<-other.order
to.plot <- to.plot[cond.order,]


# standardized effect
effectssd <- c(racial=round((coef(reg02)[4]+coef(reg02)[5])/sd(dturk$OUTsupport,na.rm=T),2),
			control=round((coef(reg02)[4])/sd(dturk$OUTsupport,na.rm=T),2),
			regional=round((coef(reg02)[4]+coef(reg02)[6])/sd(dturk$OUTsupport,na.rm=T),2) )#effects in SD
	
# p-value of conditional relative unconditional		
ps <- round(c(racial=t.test(OUTsupport~Tcond,data=subset(reg02$model,TO=="Racial"))$p.value,
		control=t.test(OUTsupport~Tcond,data=subset(reg02$model,TO=="Control"))$p.value,
		regional=t.test(OUTsupport~Tcond,data=subset(reg02$model,TO=="Regional"))$p.value),3)
 
# p-value of premium in control relative to racial and regional
pps <-  round(robse(reg02)[c(5,4,6),"robpval"],3)
pps[2] <- NA

pdf(file=paste(savedir,"/fig-Turkey-P01S03-3x2barplot.pdf",sep=""))
par(mar=c(5,4.5,2,1))
tmp <- barplot(to.plot,beside=T,ylim=c(1,7), xpd=F)
polygon(x=c(3.5,6.5,6.5,3.5),y=c(0,0,8,8),border=NA,col=gray(0.8))
mtext(text="Support for Social Transfer Program",side=2,line=3)
#abline(h=to.plot[,1],lty=1,col=gray(0.8))
par(new=T)
barplot(to.plot,beside=T,
		ylim=if(max(predict(reg02))<1){c(0,1)}else{c(1,7)}, 
		xpd=F,legend.text= c("Non-conditional","Conditional"),
		args.legend = list(bty="n",cex=1.2))	
segments(x0=tmp[2,],x1=tmp[2,],
		y0=Pcond$lwr,
		y1=Pcond$upr,col=1,lwd=1)
axis(side=1,at=c(tmp[,c(2,1,3)]),
		tick=F,
		line=-1,
		labels=paste("N=", c(table(reg02$model$Tcond,reg02$model$TO)),sep=""))
text(x=tmp[2,],y=Pcond[,"upr"]
	,labels=paste("Effect = ",sprintf("%01.2f",effectssd),
			"sd\n(p=",
			sprintf("%01.2f",ps),")",sep="")
	,pos=3)
dev.off()




### The analysis that follows below is the same, but includes non-attentive respondents. 
### A variation of this figure is in the supplemental information ###

reg02 <- lm(OUTsupport~TO*Tcond,data=subset(dturk,CCTbeneficiary==F))

print(summary(reg02))

other.order <- c("Racial","Control","Regional")
cond.order <- c("Unconditional","Conditional")

Puncond <- data.frame(predict(reg02,newdata=data.frame(
			Tcond=rep("_Uncond",3),
			TO=other.order),
			interval="confidence"))
Pcond <- data.frame(predict(reg02,newdata=data.frame(
			Tcond=rep("Cond",3),
			TO=other.order),
			interval="confidence"))

to.plot <- rbind(Conditional=Pcond$fit,
				Unconditional=Puncond$fit)
colnames(to.plot)<-other.order
to.plot <- to.plot[cond.order,]


# standardized effect
effectssd <- c(racial=round((coef(reg02)[4]+coef(reg02)[5])/sd(dturk$OUTsupport,na.rm=T),2),
			control=round((coef(reg02)[4])/sd(dturk$OUTsupport,na.rm=T),2),
			regional=round((coef(reg02)[4]+coef(reg02)[6])/sd(dturk$OUTsupport,na.rm=T),2) )#effects in SD
	
# p-value of conditional relative unconditional		
ps <- round(c(racial=t.test(OUTsupport~Tcond,data=subset(reg02$model,TO=="Racial"))$p.value,
		control=t.test(OUTsupport~Tcond,data=subset(reg02$model,TO=="Control"))$p.value,
		regional=t.test(OUTsupport~Tcond,data=subset(reg02$model,TO=="Regional"))$p.value),3)
 
# p-value of premium in control relative to racial and regional
pps <-  round(robse(reg02)[c(5,4,6),"robpval"],3)
pps[2] <- NA

pdf(file=paste(savedir,"/fig-Turkey-P01S03SI-3x2barplot.pdf",sep=""))
par(mar=c(5,4.5,2,1))
tmp <- barplot(to.plot,beside=T,ylim=c(1,7), xpd=F)
polygon(x=c(3.5,6.5,6.5,3.5),y=c(0,0,8,8),border=NA,col=gray(0.8))
mtext(text="Support for Social Transfer Program",side=2,line=3)
#abline(h=to.plot[,1],lty=1,col=gray(0.8))
par(new=T)
barplot(to.plot,beside=T,
		ylim=if(max(predict(reg02))<1){c(0,1)}else{c(1,7)}, 
		xpd=F,legend.text= c("Non-conditional","Conditional"),
		args.legend = list(bty="n",cex=1.2))	
segments(x0=tmp[2,],x1=tmp[2,],
		y0=Pcond$lwr,
		y1=Pcond$upr,col=1,lwd=1)
axis(side=1,at=c(tmp[,c(2,1,3)]),
		tick=F,
		line=-1,
		labels=paste("N=", c(table(reg02$model$Tcond,reg02$model$TO)),sep=""))
text(x=tmp[2,],y=Pcond[,"upr"]
	,labels=paste("Effect = ",sprintf("%01.2f",effectssd),
			"sd\n(p=",
			sprintf("%01.2f",ps),")",sep="")
	,pos=3)
dev.off()




### Analytic Results ####
### Think about creating tables with results and with controls ####
get.main.effects <- function(x,main="Conditionality"){
	tmp <- coef(x)
	the.var <- grep("^T",names(tmp))
	effects <- tmp[the.var]
	effectssd <- effects/sd(x$model$OUTsupport)
	names(effects)<-names(effectssd)<-main
	pvals <- round(linearHypothesis(x,c(names(coef(x))[the.var]),white.adjust="hc2")$'Pr(>F)'[2],3)
	rbind(effectssd,effects,pvals)
	}
get.int.effects <- function(x){
	tmp <- coef(x)
	effects <- tmp["TcondCond"]+c(tmp["TORacial:TcondCond"],0,tmp["TORegional:TcondCond"])
	effectssd <- effects/sd(x$model$OUTsupport)
	names(effects)<-names(effectssd)<-c("Racial","Control","Regional")
	pvals <- round(c(linearHypothesis(x,c("TORacial:TcondCond+TcondCond"),white.adjust="hc2")$'Pr(>F)'[2],
				linearHypothesis(x,c("TcondCond"),white.adjust="hc2")$'Pr(>F)'[2],
				linearHypothesis(x,c("TORegional:TcondCond+TcondCond"),white.adjust="hc2")$'Pr(>F)'[2]),3)
	rbind(effectssd,effects,pvals)
	}


reg00 <- lm(OUTsupport~ Tcond,data=subset(dturk,CCTbeneficiary==F))
reg00ca <- lm(OUTsupport~ Tcond+attention,data=subset(dturk,CCTbeneficiary==F))
reg00cc <- lm(OUTsupport~ Tcond+attention+wealthfac+gender,data=subset(dturk,CCTbeneficiary==F))

merge(merge(get.main.effects(reg00),get.main.effects(reg00ca),get.main.effects(reg00ca),by=0)
	
reg02 <- lm(OUTsupport~TO*Tcond,data=subset(dturk,CCTbeneficiary==F))
reg02ca <- lm(OUTsupport~TO*Tcond+attention,data=subset(dturk,CCTbeneficiary==F))
reg02cc <- lm(OUTsupport~TO*Tcond+attention+wealthfac+gender,data=subset(dturk,CCTbeneficiary==F))


get.effects(reg02)
	
# Examine similarity issues #
# Rather odd that maniupation for region decreased perception of similarty but
# increased support. If this is true, the opposite mechanism might be in place in
# Uruguay, whereby the most different is the one you want to transfer to
summary(lm(OUTsupport~OUTsimilarity,data=d))
summary(lm(OUTsimilarity~OUTredistribution,data=d))
summary(lm(OUTsimilarity~TO*Tcond,data=d))





###### ANALYSIS OF THE FULL SAMPLE HETEROGENOUS EFFECTS  ######

### Analysis ####
library(RItools)
library(xtable)
setwd("~/Dropbox/Data/Paper-GDN/Analysis-Paper-01")
savedir <- "~/Dropbox/LatexFiles/Paper-GDN/FiguresPaper01"
load("~/Dropbox/data/Paper-GDN/dataTurkeyDirect.all.RData")


### Keep only those in the otherness control manipulation
### (because disadvantaged were all stirred to this conditoin)
### Keep only attentive and NON CCT respondents (symmetry with main anaylsy)
dall <- subset(dallturk,TO=="Control"&
								attention==T&CCTbeneficiary==F)

lm(OUTsupport~Tcond,data=dall)
lm(OUTsupport~Tcond*advantaged,data=dall)
lm(OUTsupport~Tcond*wealthfac,data=dall)
lm(OUTsupport~Tcond*wealthadd,data=dall)	